package com.example.tianqi.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tianqi.R;
import com.example.tianqi.bean.WeatherCityBean;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;

public class CityAdapter extends RecyclerView.Adapter<CityAdapter.ViewHolder> {

    private final ArrayList<String> arrayList;
    //删除键
    private OnItemClickListener onItemClickListener = null;

    public CityAdapter(ArrayList<String> arrayList) {

        this.arrayList = arrayList;

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        String name = arrayList.get(position);
        holder.nameTextView.setText(name);
        OkGo.<String>get("https://tianqiapi.com/api?version=v1&appid=65716696&appsecret=pPQF7EgL&city=" + name)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        WeatherCityBean bean = new Gson().fromJson(response.body(), WeatherCityBean.class);
                        holder.temTextView.setText(bean.getData().get(0).getTem());
                        holder.airTextView.setText("空气：" + bean.getData().get(0).getAirLevel());
                        holder.airNameTextView.setText(bean.getData().get(0).getWeaNight());
                    }
                });

        holder.mainRelativeLayout.setOnClickListener(view -> {
            if (onItemClickListener != null) onItemClickListener.onClick(position, name);
        });

        holder.deleteImageView.setOnClickListener(view -> {
            if (onItemClickListener != null) onItemClickListener.onClickDelete(position, name);
        });

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup group, int viewType) {
        View view = LayoutInflater.from(group.getContext()).inflate(R.layout.item_city, group, false);
        return new ViewHolder(view);
    }

    @Override
    public int getItemCount() {

        return arrayList.size();

    }

    //删除键
    @SuppressWarnings("unused")
    public void setOnItemClickListener(OnItemClickListener listener) {

        this.onItemClickListener = listener;

    }
    @SuppressWarnings("unused")
    public interface OnItemClickListener {

        void onClick(int position, String bean);

        void onClickDelete(int position, String bean);

    }



    static class ViewHolder extends RecyclerView.ViewHolder {

        private final RelativeLayout mainRelativeLayout;
        private final AppCompatTextView nameTextView;
        private final AppCompatTextView airTextView;
        private final AppCompatTextView temTextView;
        private final AppCompatTextView airNameTextView;
        private final AppCompatImageView deleteImageView;

        private ViewHolder(View view) {
            super(view);
            mainRelativeLayout = view.findViewById(R.id.mainRelativeLayout);
            nameTextView = view.findViewById(R.id.nameTextView);
            airTextView = view.findViewById(R.id.airTextView);
            temTextView = view.findViewById(R.id.temTextView);
            airNameTextView = view.findViewById(R.id.airNameTextView);
            deleteImageView = view.findViewById(R.id.deleteImageView);
        }

    }

}
